package com.gjy.spring.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-01-23 17:09:03
 */
@Component
public class GracefulShutdownListener implements ApplicationListener<ContextClosedEvent> {

    private static final Logger log = LoggerFactory.getLogger(GracefulShutdownListener.class);

    @Override
    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        // 执行必要的清理操作
        log.info("Starting graceful shutdown...");
        // 模拟一些异步任务的等待
        try {
            Thread.sleep(1000); // 等待5秒钟模拟某些清理任务
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }    // 可能包括关闭线程池、释放资源等
        log.info("Graceful shutdown completed.");
    }
}
